package graphguide;

import java.util.ArrayList;
import java.util.Comparator;

public class GraphNode implements GeneralValueNode {

	private final String SPLIT_REGEX = "[^0-9a-zA-Z]+";

	public static final int GRAPHTYPE = 0; // 0 for literal Nodes and 1 for
											// entity Nodes

	public int type = GRAPHTYPE;
	public String name;
	public String relationPathFromParent;
	public GraphNode parent;
	public int level;

	public String normalizedName;

	private String pathFromTheRoot = null;

	public GraphNode(int type, String name, String relation, GraphNode parent,
			int level) {
		this.type = type; // literal or entity node
		this.name = name;
		this.relationPathFromParent = relation;
		this.parent = parent;
		this.level = level;

		if (type == 0)
			this.normalizedName = normalization(name);
	}

	/*    not used    */
	private ArrayList<GraphNode> getPathToNode() {
		GraphNode p = parent;
		ArrayList<GraphNode> list = new ArrayList<GraphNode>();
		list.add(this);
		while (p != null) {
			list.add(p);
			p = p.parent;
		}

		return list;
	}

	
	/*    not used    */
	public String getStringPathRootToNode() {
		if (pathFromTheRoot != null)
			return pathFromTheRoot;
		ArrayList<GraphNode> path = getPathToNode();
		StringBuffer b = new StringBuffer();
		for (GraphNode n : path)
			b.append(n.name + " " + n.relationPathFromParent + " ");
		pathFromTheRoot = b.toString();
		return pathFromTheRoot;
	}



	/*********************   COMPARE   ******************************/
	
	static class SimpleCompare implements Comparator<GraphNode> {
		@Override
		public int compare(GraphNode gn1, GraphNode gn2) {
			return gn1.name.compareToIgnoreCase(gn2.name);
		}

	};
	
	public static class NormalizeCompare implements Comparator<GraphNode> {
		@Override
		public int compare(GraphNode gn1, GraphNode gn2) {
			return gn1.normalizedName.compareToIgnoreCase(gn2.normalizedName);
		}

	}
	/**** COMPARE the normalized values of a GrahpNode and a ValueNode ****/
	public static class NormalizeCompareMIX implements Comparator<GeneralValueNode> {
		@Override
		public int compare(GeneralValueNode gn1, GeneralValueNode gn2) {
			return gn1.getNormalizedNodeValue().compareToIgnoreCase(gn2.getNormalizedNodeValue());
		}

	}
	
	/****** String Normalization *********/
	
	@Override
	public String normalization(String value) {
		String lower = value.trim().toLowerCase();
		String[] words = lower.split(SPLIT_REGEX);

		for (int i = 0; i < words.length; i++) {
			for (int j = i + 1; j < words.length; j++) {
				if (words[i].compareTo(words[j]) > 0) {
					String temp = words[i];
					words[i] = words[j];
					words[j] = temp;
				}
			}// for
		}// for
		String normalizedString = new String();
		for (int i = 0; i < words.length; i++) {
			if (i == words.length - 1)
				normalizedString = normalizedString + words[i];
			else
				normalizedString = normalizedString + words[i] + " ";
		}
		return normalizedString;
	}

	@Override
	public String getNormalizedNodeValue() {
		
		return this.normalizedName;
	}

	

}
